﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFMysql;
using CustomMVC;
using System.Diagnostics;
using CommonLib;

namespace BLL
{
    public class SysBll:BaseBll
    {
        public List<pre_shipingtype> GetOrderTypeList(pre_shipingtype orderType, int pageSize, int pageNum, ref int totalRecord)
        {
            List<Conditioner> condition = new List<Conditioner>();
            if (!string.IsNullOrEmpty(orderType.spname))
            {
                condition.Add(new Conditioner { Key = "spname", Value = orderType.spname, Contract = "=" });
            }
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_shipingtype.AsQueryable();
                    List<pre_shipingtype> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    totalRecord = list.Count;  
                    return list.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public List<pre_group> GetGroupList(pre_group group, int pageSize, int pageNum, ref int totalRecord)
        {
            List<Conditioner> condition = new List<Conditioner>();
            if (!string.IsNullOrEmpty(group.TITLE))
            {
                condition.Add(new Conditioner { Key = "spname", Value = group.TITLE, Contract = "=" });
            }
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_group.AsQueryable();
                    List<pre_group> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    totalRecord = list.Count;  
                    return list.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public bool CheckGroupTitle(string title)
        {
            List<Conditioner> condition = new List<Conditioner>();
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_group.AsQueryable();
                    List<pre_group> list = LinqHelperExtension.Where(query, new List<Conditioner> { new Conditioner { Key = "TITLE", Value = title, Contract = "=" } }.ToArray()).ToList();
                    return list.Count > 0 ? false : true;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public bool CheckFuntionID(string funcId)
        {
            List<Conditioner> condition = new List<Conditioner>();
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_function.AsQueryable();
                    List<pre_function> list = LinqHelperExtension.Where(query, new List<Conditioner> { new Conditioner { Key = "FUNCID", Value = funcId, Contract = "=" } }.ToArray()).ToList();
                    return list.Count > 0 ? false : true;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public List<pre_function> GetFunctionList(pre_function function, int pageSize, int pageNum, ref int totalRecord)
        {
            List<Conditioner> condition = new List<Conditioner>();
            if (!string.IsNullOrEmpty(function.REMARK))
            {
                condition.Add(new Conditioner { Key = "REMARK", Value = function.REMARK, Contract = "=" });
            }
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_function.AsQueryable();
                    List<pre_function> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    totalRecord = list.Count;  
                    return list.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public List<pre_user> GetUserList(pre_user user, int pageSize, int pageNum, ref int totalRecord)
        {
            List<Conditioner> condition = new List<Conditioner>();
            if (!string.IsNullOrEmpty(user.username))
            {
                condition.Add(new Conditioner { Key = "username", Value = user.username, Contract = "=" });
            }
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_user.AsQueryable();
                    List<pre_user> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    totalRecord = list.Count;                    
                    return list.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public List<pre_cost> GetCostList(pre_cost cost, int pageSize, int pageNum, ref int totalRecord)
        {
            List<Conditioner> condition = new List<Conditioner>();
            if (cost.shippingtype.HasValue)
            {
                condition.Add(new Conditioner { Key = "shippingtype", Value = cost.shippingtype.Value.ToString(), Contract = "=" });
            }
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_cost.AsQueryable();
                    List<pre_cost> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    totalRecord = list.Count;  
                    return list.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public List<pre_shipingtype> GetOrderType()
        {
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_shipingtype.AsQueryable();
                    List<pre_shipingtype> list = LinqHelperExtension.Where(query, new Conditioner[] { }).ToList();
                    return list.Count > 0 ? list : null;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }

        public List<pre_menu> GetMenus()
        {
            List<Conditioner> condition = new List<Conditioner>();          
            try
            {
                using (Model1 db = new Model1())
                {
                    var query = db.pre_menu.AsQueryable();
                    List<pre_menu> list = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                throw ex;
            }
        }        
    }
}
